package com.tmeone.erpone.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.entity.SCompany;
import com.tmeone.erpone.entity.SRole;
import com.tmeone.erpone.entity.SUser;
import com.tmeone.erpone.entity.TProduct;
import com.tmeone.erpone.service.ISCompanyService;
import com.tmeone.erpone.service.ISUserService;
import com.tmeone.erpone.util.MyUser;
import com.tmeone.erpone.service.ITRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tmeone
 * @since 2021-04-18
 */
@RestController
public class SCompanyController {
    @Autowired
    private ISCompanyService companyService;

    @Autowired
    private ITRelationService relationService;

    @Autowired
    private MyUser myUser;

    /**
     * by wzs
     */
    @Autowired
    private ISUserService userService;

    private static String companyId;

    @ResponseBody
    @RequestMapping("/companys")
    public IPage<SCompany> selectListRole(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current) {
        try {
            IPage<SCompany> arg = new Page<>(current, 5);
            IPage<SCompany> page = companyService.pageSCompanys(arg);
            return page;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @RequestMapping(value = "/deta/{id}", method = RequestMethod.GET)
    public void detailsCompany(@PathVariable String id) {
        this.companyId = id;
        System.out.println("公司ID:" + id);
    }

    @ResponseBody
    @RequestMapping("/company/products")
    public IPage<TProduct> selectListProduct(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current) {
        try {
            IPage<TProduct> arg = new Page<>(current, 5);
            IPage<TProduct> page = companyService.pageTProduct(arg, companyId);
            return page;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @ResponseBody
    @RequestMapping("/company")
    public SCompany selectCompany() {
        SCompany company1 = new SCompany();
        List<SCompany> list = companyService.list();
        for (SCompany company : list) {
            if (company.getId().equals(companyId)) {
                company1 = company;
            }
        }
        return company1;
    }

    //查询所有公司
    @GetMapping("/companies")
    public JsonResult getAllCompanies() {
        return new JsonResult(1, companyService.list());
    }

    @RequestMapping(value = "/companyUpdate", method = RequestMethod.PUT)
    public void companyRemak(@RequestBody SCompany company) {
        relationService.updateRemark(company.getId(), company.getRRemark());
        System.out.println("修改：" + company.toString());
        //companyService.updateById(company);
    }

    @ResponseBody
    @RequestMapping("/companyselect")
    public IPage<SCompany> companySelect(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                         @RequestParam(value = "prov", required = false, defaultValue = "") String prov,
                                         @RequestParam(value = "city", required = false, defaultValue = "") String city,
                                         @RequestParam(value = "district", required = false, defaultValue = "") String district,
                                         @RequestParam(value = "input", required = false, defaultValue = "") String input) {

        System.out.println("这是一个测试数据：" + current + "_____" + prov + "______" + city + "______" + district + "_____" + input);
        try {
            IPage<SCompany> arg = new Page<>(current, 5);
            IPage<SCompany> page = companyService.pageUcompany(arg, prov, city, district, input);
            return page;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    @ResponseBody
    @RequestMapping(value = "/comuser/companyName1/{name}",method = RequestMethod.GET)
    public String companyName(@PathVariable String name) {
        List<SCompany> list = companyService.list();
        String str = "";
        for (SCompany sCompany : list) {
            if (sCompany.getCompanyName().equals(name)) {
                str = "该企业名不可用,请重新输入！";
                return str;
            }
        }
        str = "正常";
        return str;
    }

    @RequestMapping(value = "/comuser/company", method = RequestMethod.POST)
    public String companyRegisterin(@RequestBody SCompany company) {
        System.out.println("这是一个注册测试：" + company.toString());
        company.setId(UUID.randomUUID().toString().replace("-", "").substring(1, 11));
        companyService.save(company);
        String substring = UUID.randomUUID().toString().replace("-", "").substring(1, 10);
        return substring;
    }

    /**
     * 查询所有供应商
     * 作者：wzs
     * @return 所有供应商
     */
    @GetMapping("/suppliers")
    public JsonResult suppliers() {
        // 获取当前用户
        SUser user = myUser.getUser();

        QueryWrapper<SCompany> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("s_company.*");
        String endSql = "LEFT JOIN t_relation r ON s_company.id = r.supplier_id LEFT JOIN s_company c ON c.id = r.customer_id WHERE c.id = ";
        queryWrapper2.last(endSql+"'"+user.getCompanyId()+"'");
        return new JsonResult(1,companyService.list(queryWrapper2));
    }
}
